define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        // {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'LIKE'},
                        // {field: 'goods_amount', title: __('Goods_amount'), operate:'BETWEEN'},
                        {field: 'order_amount', title: __('Order_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"9":__('Status 9')}, formatter: Table.api.formatter.status},
                        // {field: 'pay_status', title: __('Pay_status'), searchList: {"1":__('Pay_status 1'),"2":__('Pay_status 2'),"3":__('Pay_status 3')}, formatter: Table.api.formatter.status},
                        {field: 'ship_status', title: __('Ship_status'), searchList: {"1":__('Ship_status 1'),"2":__('Ship_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'cost_freight', title: __('Cost_freight'), operate:'BETWEEN'},
                        // {field: 'refund_fail_reason', title: __('Refund_fail_reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_id', title: __('User_id'), formatter: function (value, row) {
                                return row.user.username;
                            }},
                        {field: 'confirm', title: __('Confirm'), searchList: {"1":__('Confirm 1'),"2":__('Confirm 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'confirmtime', title: __('Confirmtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'ship_area_id', title: __('Ship_area_id')},
                        // {field: 'ship_area', title: __('Ship_area'), operate: 'LIKE'},
                        {field: 'ship_address', title: __('Ship_address'), operate: false},
                        {field: 'ship_name', title: __('Ship_name'), operate: 'LIKE'},
                        {field: 'ship_mobile', title: __('Ship_mobile')},
                        // {field: 'ship_c_code', title: __('Ship_c_code')},
                        // {field: 'ship_c_name', title: __('Ship_c_name')},
                        // {field: 'order_pmt', title: __('Order_pmt'), operate:'BETWEEN'},
                        // {field: 'goods_pmt', title: __('Goods_pmt'), operate:'BETWEEN'},
                        // {field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'ip', title: __('Ip'), operate: 'LIKE'},
                        // {field: 'mark', title: __('Mark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'transaction_id', title: __('Transaction_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        // {field: 'jieziarea.name', title: __('Jieziarea.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            ,buttons:[
                                // {
                                //     name: 'set_status',
                                //     title: function (e){
                                //         return '设为' + (e.status == 1 ? '已支付' : '未支付');
                                //     },
                                //     text: function (e){
                                //         return '设为' + (e.status == 1 ? '已支付' : '未支付');
                                //     },
                                //     classname: 'btn btn-xs btn-success btn-ajax',
                                //     icon: 'fa fa-list',
                                //     url: function(e){
                                //         return 'order/set_status/ids/' +e.id + '/status/' + (e.status == 1 ? '2' : '1')
                                //     },
                                //     refresh: true,
                                //     visible: function (e){
                                //         return (e.status == 1 || e.status == 2) ? true : false;
                                //     }
                                // },
                                // {
                                //     name: 'refund',
                                //     title: '退款',
                                //     text: '退款',
                                //     classname: 'btn btn-xs btn-success btn-ajax',
                                //     icon: 'fa fa-leaf',
                                //     confirm: '确认退款',
                                //     refresh: true,
                                //     url: function(e){
                                //         return 'order/refund/ids/' +e.id
                                //     },
                                //     visible: function (e){
                                //         return (e.pay_status == 2 && e.confirm == 1) ? true : false;
                                //     }
                                // },
                                {
                                    name: 'info',
                                    title: '详细',
                                    text: '详细',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-leaf',
                                    refresh: true,
                                    url: function(e){
                                        return 'order/info/ids/' +e.id
                                    },
                                    visible: function (e){
                                        return true;
                                        return (e.status == 2) ? true : false;
                                    }
                                },
                                {
                                    name: 'receive',
                                    title: '收货',
                                    text: '收货',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '确认收货',
                                    refresh: true,
                                    url: function(e){
                                        return 'order/receive/ids/' +e.id
                                    },
                                    visible: function (e){
                                        return (e.status == 3) ? true : false;
                                    }
                                },
                                {
                                    name: 'shipping',
                                    title: '发货',
                                    text: '发货',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-leaf',
                                    refresh: true,
                                    url: function(e){
                                        return 'order/shipping/ids/' +e.id
                                    },
                                    visible: function (e){
                                        return (e.status == 1) ? true : false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        shipping: function () {
            $('#c-ship_c_name').data('eSelect', function (data) {
                console.log('data', data)
                $('#c-ship_c_code').val(data.delivery_id);
                console.log('fasfds', $('#c-ship_c_code').val())
            })
            Controller.api.bindevent();
        },
        info: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
